home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / machserver / tests / malloc / test.c < prev   
Encoding:
C/C++ Source or Header  |  1991-12-13  |  3.8 KB  |  184 lines

  1. /* 
  2.  * test.c --
  3.  *
  4.  *    Some tests for malloc().
  5.  *
  6.  * Copyright 1991 Regents of the University of California
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that this copyright
  10.  * notice appears in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  */
  15.  
  16. #ifndef lint
  17. static char rcsid[] = "$Header: /r3/kupfer/spriteserver/tests/malloc/RCS/test.c,v 1.1 91/12/12 22:39:07 kupfer Exp $ SPRITE (Berkeley)";
  18. #endif /* not lint */
  19.  
  20. #include <bstring.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <sys/wait.h>
  25. #include <vm.h>
  26.  
  27. #define PARENT_DOES_MALLOC    1
  28.  
  29. /* Forward references: */
  30.  
  31. void CheckBuffer(), FillBuffer();
  32. void DoChild(), DoParent();
  33.  
  34. main()
  35. {
  36.     char *buf;
  37.     int pageSize;
  38.     int fds[2];            /* descriptors for pipe */
  39.     int childPid;
  40.  
  41. #if PARENT_DOES_MALLOC
  42.     /* Try allocating something smaller than a page. */
  43.     buf = malloc(100);
  44.     strcpy(buf, "small buffer");
  45.     bzero(buf, 100);
  46.     free(buf);
  47.  
  48.     /* Try allocating an entire page. */
  49.     Vm_PageSize(&pageSize);
  50.     buf = malloc(pageSize);
  51.     bzero(buf, pageSize);
  52.  
  53.     /* 
  54.      * Write into the one-page buffer.  Fork.  Do some tests.  
  55.      */
  56.  
  57.     FillBuffer(buf, pageSize, 'a');
  58. #endif /* PARENT_DOES_MALLOC */
  59.     pipe(fds);
  60.  
  61.     childPid = fork();
  62.     switch (childPid) {
  63.     case -1:
  64.     perror("fork");
  65.     exit(1);
  66.     break;
  67.     case 0:
  68.     DoChild(buf, pageSize, fds[0]);
  69.     break;
  70.     default:
  71.     DoParent(buf, pageSize, fds[1]);
  72.     break;
  73.     }
  74.  
  75.     exit(0);
  76. }
  77.  
  78. /* 
  79.  * Verify that the child inherited the buffer correctly and can continue to
  80.  * do its own mallocs.  Verify that the parent can overwrite the buffer w/o
  81.  * the child seeing it.
  82.  */
  83. void
  84. DoChild(buf, bufSize, fd)
  85.     char *buf;
  86.     int bufSize;
  87.     int fd;            /* descriptor for pipe to parent */
  88. {
  89.     char *dummy;
  90.     char waitChar[10];
  91.  
  92. #if PARENT_DOES_MALLOC
  93.     CheckBuffer(buf, bufSize, 'a', "child");
  94. #endif
  95.  
  96. #if 0
  97.     Test_PutMessage("child: CR to continue\n");
  98.     Test_GetString(waitChar, 10);
  99. #endif
  100.     dummy = malloc(bufSize / 2);
  101.     if (dummy == NULL) {
  102.     fprintf(stderr, "child: first malloc failed.\n");
  103.     exit(1);
  104.     }
  105. #if PARENT_DOES_MALLOC
  106.     bcopy(buf, dummy, bufSize/2);
  107. #endif
  108.     free(dummy);
  109.     dummy = malloc(bufSize);
  110.     if (dummy == NULL) {
  111.     fprintf(stderr, "child: second malloc failed.\n");
  112.     exit(1);
  113.     }
  114. #if PARENT_DOES_MALLOC
  115.     bcopy(buf, dummy, bufSize);
  116. #endif
  117.     free(dummy);
  118.  
  119.     if (read(fd, dummy, 1) < 0) {
  120.     perror("child: read");
  121.     exit(1);
  122.     }
  123. #if PARENT_DOES_MALLOC
  124.     CheckBuffer(buf, bufSize, 'a', "child");
  125. #endif
  126.     free(dummy);
  127. }
  128.  
  129. void
  130. DoParent(buf, bufSize, fd)
  131.     char *buf;
  132.     int bufSize;
  133.     int fd;            /* descriptor for pipe to child */
  134. {
  135.     char dummy;
  136.     union wait status;
  137.  
  138. #if PARENT_DOES_MALLOC
  139.     CheckBuffer(buf, bufSize, 'a', "parent");
  140.     FillBuffer(buf, bufSize, 'b');
  141. #endif
  142.     if (write(fd, &dummy, 1) < 0) {
  143.     perror("parent: write");
  144.     exit(1);
  145.     }
  146. #if PARENT_DOES_MALLOC
  147.     CheckBuffer(buf, bufSize, 'b', "parent");
  148. #endif
  149.  
  150.     /* Wait for the child to exit. */
  151.     wait(&status);
  152. }
  153.  
  154. /* Fill the buffer with the given character. */
  155. void
  156. FillBuffer(buf, bufSize, ch)
  157.     char *buf;
  158.     int bufSize;
  159.     char ch;
  160. {
  161.     for (; bufSize > 0; buf++, bufSize--) {
  162.     *buf = ch;
  163.     }
  164. }
  165.  
  166. /* Verify that the buffer is filled with the given character. */
  167. void
  168. CheckBuffer(buf, bufSize, ch, who)
  169.     char *buf;
  170.     int bufSize;
  171.     char ch;
  172.     char *who;            /* "parent" or "child" */
  173. {
  174.     char *bufPtr;
  175.  
  176.     for (bufPtr = buf; bufPtr < buf + bufSize; bufPtr++) {
  177.     if (*bufPtr != ch) {
  178.         fprintf(stderr, "%s: wanted 0x%x, found 0x%x at buf[%d]\n",
  179.             who, ch, *bufPtr, bufPtr - buf);
  180.         exit(1);
  181.     }
  182.     }
  183. }
  184.